package com.hanhai.zrb.mobile.controller;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.IntentionService;
import com.hanhai.zrb.api.biz.service.UserStatisticDetailService;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.mobile.util.FrontLoginUtil;
import com.hanhai.zrb.model.common.BaseUser;
import com.hanhai.zrb.model.intention.Intention;
import com.hanhai.zrb.model.user.UserStatisticDetail;

@Controller
@RequestMapping("/intention")
public class IntentionApiController extends BaseController {

	@Resource
	private IntentionService intentionService;
	
	@Resource
	private UserStatisticDetailService userStatisticDetailService;
	
	@LoginNeedless
	@RequestMapping("/list")
	@ResponseBody
	public BaseApiResult list(Long maxId,Integer limit ) {
		
		BaseApiResult result = new BaseApiResult();
		List<Intention> list = intentionService.findListDtailesBymaxId(null, maxId, limit);
		BaseUser baseUser = FrontLoginUtil.getCurrentUser();
		if(baseUser!=null){
			List<UserStatisticDetail> usdlist =userStatisticDetailService.queryByUserIdAndType(baseUser.getUid(), UserStatisticDetail.Type.inten_col);
			Set<Long> userAttentionIds = getUserAttentionId(usdlist);
			for(Intention intention:list){
				if(userAttentionIds.contains(intention.getId())){
					intention.setAttention(1);
				}else{
					intention.setAttention(0);
				}
			}
		}
//		String data = JSON.toJSONString(list, new PropertyFilter() {
//			@Override
//			public boolean apply(Object object, String name, Object value) {
//				if(StringUtils.equals(name, "content")){
//					return false;
//				}
//				return false;
//			}
//		});
		//list = ModelViewUtil.getMapModelViewList(list,Lists.newArrayList("content"));
		//ModelViewUtil.getMapModelViewList(list,Lists.newArrayList("id","intro","industry","district","amount","mode","type","userId","pushTime","projectId","auth","attentionTimes","avatar","userName","messageAmount","org","modeName","typeName","districtName","industryName","attention","phone","authName","position"));
		result.setData(list);
		
		return result;
	}
	
	
	
	@RequestMapping("/listByUser")
	@ResponseBody
	public BaseApiResult listByUser(Long uid,Long maxId,Integer limit ) {
		
		if(maxId == null){
			maxId = Long.MAX_VALUE;
		}
		BaseApiResult result = new BaseApiResult();
		List<Intention> list = intentionService.findListDtailesBymaxId(uid, maxId, limit);
		BaseUser baseUser = FrontLoginUtil.getCurrentUser();
		if(baseUser!=null){
			List<UserStatisticDetail> usdlist =userStatisticDetailService.queryByUserIdAndType(baseUser.getUid(), UserStatisticDetail.Type.inten_col);
			Set<Long> userAttentionIds = getUserAttentionId(usdlist);
			for(Intention intention:list){
				if(userAttentionIds.contains(intention.getId())){
					intention.setAttention(1);
				}else{
					intention.setAttention(0);
				}
			}
		}
//		String data = JSON.toJSONString(list, new PropertyFilter() {
//			@Override
//			public boolean apply(Object object, String name, Object value) {
//				if(StringUtils.equals(name, "content")){
//					return false;
//				}
//				return false;
//			}
//		});
		//list = ModelViewUtil.getMapModelViewList(list,Lists.newArrayList("content"));
		//ModelViewUtil.getMapModelViewList(list,Lists.newArrayList("id","intro","industry","district","amount","mode","type","userId","pushTime","projectId","auth","attentionTimes","avatar","userName","messageAmount","org","modeName","typeName","districtName","industryName","attention","phone","authName","position"));
		result.setData(list);
		
		return result;
	}
	
	private Set<Long> getUserAttentionId(List<UserStatisticDetail> list){
		Set<Long> abc = Sets.newHashSet();
		for(UserStatisticDetail usd:list){
			abc.add(usd.getDestId());
		}
		return abc;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public BaseApiResult add(Intention intention){
		BaseApiResult result = new BaseApiResult();
		BaseUser baseUser = FrontLoginUtil.getCurrentUser();
		intention.setUserId(baseUser.getUid());
		intention.setPushTime(new Date());
		intention.setStatus(0);
		if(intentionService.saveIntention(intention)){
			result.setData(intention.getId());
			result.setMsg("发布成功!");
		}else{
			result.setRes(0);
			result.setMsg("发布失败!");
		}
		return result;
	}
	
	@RequestMapping(value="/toEdit")
	@ResponseBody
	public BaseApiResult toEdit(Long id) {
		BaseApiResult result = new BaseApiResult();
		BaseUser baseUser = FrontLoginUtil.getCurrentUser();
		Intention intention = intentionService.selectByIdAndUserId(id, baseUser.getUid());
		if(intention==null){
			result.setRes(0);
			result.setMsg("编辑失败!");
			return result;
		}
		result.setData(intentionService.getDetails(id));
		return result;
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public BaseApiResult edit(Intention intention) {
		BaseApiResult result = new BaseApiResult();
		BaseUser baseUser = FrontLoginUtil.getCurrentUser();
		Intention it = intentionService.selectByIdAndUserId(intention.getId(), baseUser.getUid());
		if(it == null || it.getProjectId() != null){
			result.setRes(0);
			result.setMsg("无法操作!信息已失效，请刷新再试");
			return result;
		}
		
		if (intentionService.updateIntention(intention)) {
			result.setMsg("编辑成功!");
		}else{
			result.setRes(0);
			result.setMsg("编辑失败!");
		}
		return result;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public BaseApiResult delete(Long id) {
		BaseApiResult result = new BaseApiResult();
		BaseUser baseUser = FrontLoginUtil.getCurrentUser();
		Intention intention = intentionService.selectByIdAndUserId(id, baseUser.getUid());
		if(intention==null ){
			result.setRes(0);
			result.setMsg("无法操作!信息已失效，请刷新再试");
			return result;
		}
		if(intention.getProjectId() != null){
			result.setRes(0);
			result.setMsg("无法操作!信息已失效，请刷新再试");
			return result;
		}
		if(intentionService.multiDelete(Lists.newArrayList(id))){
			result.setMsg("删除成功!");
		}else{
			result.setRes(0);
			result.setMsg("删除失败!");
		}
		return result;
	}
	
	
	@LoginNeedless
	@RequestMapping("/details")
	public String details(Long id,Model model){
		Intention intention = intentionService.getDetails(id);
		if((null!=intention &&intention.getStatus()==2) || null==intention){
			return "404/404";
		}
		
		model.addAttribute("intention", intention);
		return "intention/intentionDetails";
	}
	
}